package com.mt.fpb.controller;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper;
import com.mt.fpb.mapper.WechatUserMapper;
import com.mt.fpb.model.WechatUser;
import com.mt.fpb.model.vo.CommonResult;
import com.mt.fpb.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.spring.web.json.Json;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;



@Controller
@Slf4j

@RequestMapping("/wexin")
public class OpenidController {



     @Autowired
     private WechatUserMapper wechatUserMapper;

            private String APPID="wx793ceb366a83f1d4";
            private String appSecret="d78cfc27fcd6ed390d111bd8eed85f3d";
            private String url ="http://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_userinfo#wechat_redirect";
            private String openid_url ="http://api2.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
            private String userinfo_url="http://api2.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
            private String  REDIRECT_URI="http://www.rx-insurance.com";


    @GetMapping("/getOpenid")
    @ResponseBody
    public CommonResult getOpenId(String code) throws Exception {
            System.out.println(code);
            String openid_url1="";
            //获取access_token,openid,refresh_token;
            openid_url1=openid_url.replace("APPID",APPID).
                    replace("SECRET",appSecret).
                    replace("CODE",code);
            String json=HttpUtils.get(openid_url1);
            cn.hutool.json.JSONObject jsonObject=new JSONObject(json);
            String openid=(String) jsonObject.get("openid");
            String access_token=(String) jsonObject.get("access_token");
            System.out.println("获取到的access_token:"+access_token);
            System.out.println("获取到的openid:"+openid);


            //获取用户信息
            String userinfo_url1="";
            userinfo_url1=userinfo_url.replace("ACCESS_TOKEN",access_token).replace("OPENID",openid);
            String userinfo=HttpUtils.get(userinfo_url1);
            JSONObject userinfojson=new JSONObject(userinfo);
            System.out.println(userinfojson);
            int sex= (int) userinfojson.get("sex");
            String city=userinfojson.get("city").toString();
            String country=userinfojson.get("country").toString();
            String province=userinfojson.get("province").toString();
            String headimgurl=userinfojson.get("headimgurl").toString();
            String nickname=  userinfojson.get("nickname").toString();
            WechatUser wechatUser=new WechatUser(openid,nickname,sex,city,country,province,headimgurl);
            //将用户信息保存到数据库
            if(wechatUserMapper.getUser(openid)!=null){
                QueryWrapper queryWrapper=new QueryWrapper<WechatUser>();
                queryWrapper.eq("open_id",openid);
                int i=wechatUserMapper.update(wechatUser,queryWrapper);
                log.info("更新返回值:"+i);
            }
            else{
            wechatUserMapper.addUserInfo(wechatUser);
            }
            WechatUser wxuser= wechatUserMapper.getUser(openid);
            return CommonResult.success(wxuser);
        }

    }
